<?php
/**
 * Author: lf
 * Blog: https://blog.szdma.com
 * Email: job@szdma.com
 * Created at: 2016-03-21 19:36.
 */

namespace backend\grid;

use Closure;
use common\libs\Constants;
use InvalidArgumentException;
use yii;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * {@inheritdoc}
 */
class StatusColumn extends DataColumn
{
    public $format = 'raw';

    public $attribute = 'status';

    public $headerOptions = ['width' => '25px'];

    public $url = '';

    public $text = '';

    public $aOptions = [];

    public $yesClass = 'label-primary';

    public $noClass = 'label-default';

    public $formName = '';

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();

        if (empty($this->aOptions)) {
            if (false !== $this->url) {
                $this->aOptions = array_merge(
                    $this->aOptions,
                    [
                        'data-method' => 'post',
                        'data-pjax'   => '0',
                    ]
                );
            }
        }

        $this->content = function ($model, $key, $index, $gridView) {
            /* @var $model array|yii\db\ActiveRecord */
            $field = $this->attribute;
            if ('' === $this->text) {
                $text = Constants::getYesNoItems($model[$field]);
            } else {
                if ($this->text instanceof Closure) {
                    $text = call_user_func($this->text, $model, $key, $index, $gridView);
                } else {
                    $text = $this->text;
                }
            }
            if (!is_string($text)) {
                throw new InvalidArgumentException("No status valued {$model[$field]}");
            }

            if (false === $this->url) {
                $url = '';
            } else {
                if ('' == $this->url) {
                    $url = Url::to(['update', 'id' => $model['id']]);
                } else {
                    if ($this->url instanceof Closure) {
                        $url = call_user_func($this->url, $model, $key, $index, $gridView);
                    } else {
                        $url = $this->url;
                    }
                }
            }
            $aOptions = [];
            if ('' != $url) {
                if (!isset($this->aOptions['data-params'])) {
                    $aOptions = array_merge(
                        [
                            'data-params' => [
                                $this->formName ? $this->formName :
                                    (0 === strpos(strrev($model->formName()), 'hcraeS') ? strrev(substr(strrev($model->formName()), 6)) : $model->formName())
                                    ."[{$field}]" => Constants::YesNo_Yes == $model[$field] ? Constants::YesNo_No : Constants::YesNo_Yes,
                            ],
                        ],
                        $this->aOptions,
                        $aOptions
                    );
                }
                if (!isset($this->aOptions['class'])) {
                    $class = Constants::YesNo_Yes == $model[$field] ? $this->yesClass : $this->noClass;
                    $aOptions = array_merge(
                        [
                            'class' => 'label '.$class,
                        ],
                        $this->aOptions,
                        $aOptions
                    );
                }
                if (!isset($this->aOptions['data-confirm'])) {
                    $aOptions = array_merge(
                        [
                            'data-confirm' => Constants::YesNo_Yes == $model[$field] ? Yii::t('app', 'Are you sure you want to disable this item?') : Yii::t(
                                'app',
                                'Are you sure you want to enable this item?'
                            ),
                        ],
                        $this->aOptions,
                        $aOptions
                    );
                }
            }

            return Html::a($text, $url, $aOptions);
        };
    }
}
